package com.cfg;

import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 */


public abstract class AbstractServiceProxy implements ProxyInterface {
    
    protected Object target;
    protected Method targetMethod;
    protected MethodProxy methodProxy;
    protected AbstractServiceProxy nextProxy;
    protected AbstractServiceProxy lastProxy;
    
    public AbstractServiceProxy() {
    }
    
    public AbstractServiceProxy getNextProxy() {
        return this.nextProxy;
    }
    
    public void setNextProxy(ProxyInterface var1) {
        this.nextProxy = (AbstractServiceProxy)var1;
    }
    
    public void setLastProxy(ProxyInterface var1) {
        this.lastProxy = (AbstractServiceProxy)var1;
    }
    
    public Object executeMethodForMe(Object[] var1) {
        return this.executeMethod(this, var1, this.getTargetMethod().getName(), this.getTargetMethod().getParameterTypes());
    }
    
    protected Object executeMethod(Object... var1) {
        return this.nextProxy != null ? this.executeMethod(this.nextProxy, var1, this.targetMethod.getName(), this.targetMethod.getParameterTypes()) : this.executeMethodForTarget(var1);
    }
    
    private Object executeMethodForTarget(Object[] var1) {
        return this.executeMethod((Object)null, var1, (String)null, (Class[])null);
    }
    
    protected Object executeMethod(Object var1, Object[] var2, String var3, Class<?>... var4) {
        if (var1 == null) {
            try {
                return this.methodProxy.invokeSuper(this.target, var2);
            } catch (Throwable var7) {
                throw new RuntimeException(var1.getClass().getName() + "方法调用失败！", var7);
            }
        } else {
            this.syncNextProxyInfo();
            Method var5 = null;
            
            try {
                var5 = var1.getClass().getMethod(var3, var4);
            } catch (NoSuchMethodException var10) {
                throw new RuntimeException(var1.getClass().getName() + "方法获取失败！", var10);
            }
            
            if (var5 != null) {
                try {
                    return var5.invoke(var1, var2);
                } catch (IllegalAccessException var8) {
                    throw new RuntimeException(var1.getClass().getName() + "方法调用出错！", var8);
                } catch (InvocationTargetException var9) {
                    throw new RuntimeException(var1.getClass().getName() + "方法调用失败！", var9);
                }
            } else {
                return null;
            }
        }
    }
    
    protected void syncNextProxyInfo() {
        if (this.nextProxy != null) {
            this.nextProxy.setTargetMethod(this.targetMethod);
            this.nextProxy.setTarget(this.target);
            this.nextProxy.setMethodProxy(this.methodProxy);
        }
        
    }
    
    public Object getTarget() {
        return this.target;
    }
    
    public void setTarget(Object var1) {
        this.target = var1;
    }
    
    public Method getTargetMethod() {
        return this.targetMethod;
    }
    
    public void setTargetMethod(Method var1) {
        this.targetMethod = var1;
    }
    
    public AbstractServiceProxy getLastProxy() {
        return this.lastProxy;
    }
    
    public void setMethodProxy(MethodProxy var1) {
        this.methodProxy = var1;
    }
}
